import csv


def r2id():
    with open("relation2id.txt", mode="w", encoding="utf-8-sig") as f:
        f.write(str(1) + "\n")
        f.write("导致" + "\t" + "0" + "\n")


# 实体
def yue2id():
    with open("d.csv", mode='rt', encoding="utf-8-sig") as f_in:
        reader = csv.reader(f_in, delimiter=',')
        names1 = []
        idx1 = []
        # 逐行获取数据，并处理
        for line in reader:
            names1.append(line[1])
            idx1.append(line[0])
    f_in.close()
    ents1 = names1[1:]
    ids1 = idx1[1:]
    with open("s.csv", mode='rt', encoding="utf-8-sig") as f_in:
        reader = csv.reader(f_in, delimiter=',')
        names2 = []
        idx2 = []
        # 逐行获取数据，并处理
        for line in reader:
            names2.append(line[1])
            idx2.append(line[0])
    f_in.close()
    ents2 = names2[1:]
    ids2 = idx2[1:]

    ents = ents1 + ents2
    ids = ids1 + ids2
    idx = 0
    es = zip(ents, ids)
    with open("entity2id.txt", mode="w", encoding="utf-8-sig") as f:
        f.write(str(len(ents)) + "\n")
        for e in es:
            f.writelines(e[1] + "\t" + e[0] + "\t" + str(idx) + "\n")
            idx += 1


def e2id():
    with open("d.csv", mode='rt', encoding="utf-8-sig") as f_in:
        reader = csv.reader(f_in, delimiter=',')
        names1 = []
        # 逐行获取数据，并处理
        for line in reader:
            names1.append(line[1])
    f_in.close()
    ents1 = names1[1:]
    with open("s.csv", mode='rt', encoding="utf-8-sig") as f_in:
        reader = csv.reader(f_in, delimiter=',')
        names2 = []
        # 逐行获取数据，并处理
        for line in reader:
            names2.append(line[1])
    f_in.close()
    ents2 = names2[1:]

    ents = ents1 + ents2
    idx = 0
    with open("entity2id.txt", mode="w", encoding="utf-8-sig") as f:
        f.write(str(len(ents)) + "\n")
        for ent in ents:
            f.writelines(ent + "\t" + str(idx) + "\n")
            idx += 1


def t2id():
    with open("r_s-d.csv", mode='rt', encoding="utf-8-sig") as f:
        reader = csv.reader(f, delimiter=',')
        id1 = []
        id2 = []
        weights = []
        for line in reader:
            id1.append(line[0])
            id2.append(line[1])
            weights.append(line[3])
    f.close()
    id1 = id1[1:]
    id2 = id2[1:]
    disease = []
    for id in id2:
        disease.append(id[2:])
    symptom = []
    for id in id1:
        symptom.append(str(int(id[2:]) + 27))
    weights = weights[1:]
    # 338
    # print(len(id1))
    # print(len(id2))
    # print(len(weights))
    # print(disease)
    # print(symptom)
    # print(weights)
    ts = zip(disease, symptom, weights)
    with open("uncertainty.txt", mode="w", encoding="utf-8") as f:
        f.write(str(len(id1)) + "\n")
        for t in ts:
            # print(t)
            f.writelines(t[0] + "\t" + t[1] + "\t" + t[2] + "\n")

# e2id()
t2id()
